/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.moilioncircle.wings.devops.init;

import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.io.File;


/**
 * drag to NetBeans-12.3 to edit this Frame
 *
 * @author trydofor
 */
public class WingsInitProjectSwing extends javax.swing.JFrame {

    /**
     * Creates new form Wings0InitProject
     */
    public WingsInitProjectSwing() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlPath = new javax.swing.JPanel();
        btnWingsPath = new javax.swing.JButton();
        btnProjectPath = new javax.swing.JButton();
        txtWingsPath = new javax.swing.JTextField();
        txtProjectPath = new javax.swing.JTextField();
        pnlName = new javax.swing.JPanel();
        lblGroupId = new javax.swing.JLabel();
        txtGroupId = new javax.swing.JTextField();
        lblArtifactId = new javax.swing.JLabel();
        txtArtifactId = new javax.swing.JTextField();
        lblPackage = new javax.swing.JLabel();
        txtPackage = new javax.swing.JTextField();
        lblCodeName = new javax.swing.JLabel();
        txtCodeName = new javax.swing.JTextField();
        lblWingsVer = new javax.swing.JLabel();
        txtWingsVer = new javax.swing.JTextField();
        pnlGenerator = new javax.swing.JPanel();
        btnGenerate = new javax.swing.JButton();
        pgbGenerate = new javax.swing.JProgressBar();
        jspMessage = new javax.swing.JScrollPane();
        txaMessage = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("WingsTemplate"); // NOI18N

        pnlPath.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        btnWingsPath.setText("WingsDir");

        btnProjectPath.setText("ProjectDir");

        txtWingsPath.setText("wings");

        txtProjectPath.setText("./");

        javax.swing.GroupLayout pnlPathLayout = new javax.swing.GroupLayout(pnlPath);
        pnlPath.setLayout(pnlPathLayout);
        pnlPathLayout.setHorizontalGroup(
            pnlPathLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPathLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlPathLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnWingsPath, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnProjectPath, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnlPathLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtWingsPath)
                    .addComponent(txtProjectPath))
                .addContainerGap())
        );
        pnlPathLayout.setVerticalGroup(
            pnlPathLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPathLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlPathLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtWingsPath, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnWingsPath, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlPathLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtProjectPath, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnProjectPath, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pnlName.setBorder(javax.swing.BorderFactory.createTitledBorder("properties"));

        lblGroupId.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblGroupId.setText("groupId");

        txtGroupId.setText("com.trydofor.sharing");

        lblArtifactId.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblArtifactId.setText("artifactId");

        txtArtifactId.setText("goodman");

        lblPackage.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblPackage.setText("package");

        txtPackage.setText("com.trydofor.sharing.goodman");

        lblCodeName.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblCodeName.setText("codeName");

        txtCodeName.setText("Good");

        lblWingsVer.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblWingsVer.setText("wingsVer");

        txtWingsVer.setText("2.4.2.100-SNAPSHOT");

        javax.swing.GroupLayout pnlNameLayout = new javax.swing.GroupLayout(pnlName);
        pnlName.setLayout(pnlNameLayout);
        pnlNameLayout.setHorizontalGroup(
            pnlNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlNameLayout.createSequentialGroup()
                .addGroup(pnlNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblWingsVer, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(pnlNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(pnlNameLayout.createSequentialGroup()
                            .addGap(17, 17, 17)
                            .addComponent(lblGroupId, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlNameLayout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(pnlNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(lblArtifactId, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(lblPackage, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(lblCodeName, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtGroupId)
                    .addComponent(txtArtifactId)
                    .addComponent(txtPackage, javax.swing.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE)
                    .addComponent(txtCodeName)
                    .addComponent(txtWingsVer, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        pnlNameLayout.setVerticalGroup(
            pnlNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlNameLayout.createSequentialGroup()
                .addGap(9, 9, 9)
                .addGroup(pnlNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtGroupId, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblGroupId, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtArtifactId, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblArtifactId, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPackage, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtPackage, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCodeName, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCodeName, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblWingsVer, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtWingsVer, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnGenerate.setText("Generate");

        txaMessage.setColumns(20);
        txaMessage.setRows(5);
        jspMessage.setViewportView(txaMessage);

        javax.swing.GroupLayout pnlGeneratorLayout = new javax.swing.GroupLayout(pnlGenerator);
        pnlGenerator.setLayout(pnlGeneratorLayout);
        pnlGeneratorLayout.setHorizontalGroup(
            pnlGeneratorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlGeneratorLayout.createSequentialGroup()
                .addGroup(pnlGeneratorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlGeneratorLayout.createSequentialGroup()
                        .addComponent(btnGenerate, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(pgbGenerate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(pnlGeneratorLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jspMessage)))
                .addContainerGap())
        );
        pnlGeneratorLayout.setVerticalGroup(
            pnlGeneratorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlGeneratorLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlGeneratorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(btnGenerate, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pgbGenerate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jspMessage)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnlGenerator, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnlPath, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnlName, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlPath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(pnlName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(pnlGenerator, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(() -> showWings(args));
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnGenerate;
    private javax.swing.JButton btnProjectPath;
    private javax.swing.JButton btnWingsPath;
    private javax.swing.JScrollPane jspMessage;
    private javax.swing.JLabel lblArtifactId;
    private javax.swing.JLabel lblCodeName;
    private javax.swing.JLabel lblGroupId;
    private javax.swing.JLabel lblPackage;
    private javax.swing.JLabel lblWingsVer;
    private javax.swing.JProgressBar pgbGenerate;
    private javax.swing.JPanel pnlGenerator;
    private javax.swing.JPanel pnlName;
    private javax.swing.JPanel pnlPath;
    private javax.swing.JTextArea txaMessage;
    private javax.swing.JTextField txtArtifactId;
    private javax.swing.JTextField txtCodeName;
    private javax.swing.JTextField txtGroupId;
    private javax.swing.JTextField txtPackage;
    private javax.swing.JTextField txtProjectPath;
    private javax.swing.JTextField txtWingsPath;
    private javax.swing.JTextField txtWingsVer;
    // End of variables declaration//GEN-END:variables

    private static void showWings(String[] args) {
        WingsInitProjectSwing project = new WingsInitProjectSwing();
        project.initWings(args);

        project.pack();
        project.setLocationRelativeTo(null); // 居中
        // 焦点
        project.setState(Frame.NORMAL);
        project.toFront();
        project.requestFocus();

        project.setVisible(true);
    }

    private void initWings(String[] args) {
        if (args.length > 0) {
            txtWingsPath.setText(args[0]);
        }
        if (args.length > 1) {
            txtProjectPath.setText(args[1]);
        }
        if (args.length > 2) {
            txtWingsVer.setText(args[2]);
        }

        btnWingsPath.addActionListener(evt -> chooseDir("wings-example工程目录", txtWingsPath));
        btnProjectPath.addActionListener(evt -> chooseDir("新建工程目录", txtProjectPath));
        btnGenerate.addActionListener(evt -> generatePrj());

        FocusAdapter txtFocus = new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                ((JTextField) e.getSource()).selectAll();
            }
        };
        txtWingsPath.addFocusListener(txtFocus);
        txtProjectPath.addFocusListener(txtFocus);
        txtGroupId.addFocusListener(txtFocus);
        txtArtifactId.addFocusListener(txtFocus);
        txtPackage.addFocusListener(txtFocus);
        txtCodeName.addFocusListener(txtFocus);
    }

    private void chooseDir(String name, JTextField field) {
        JFileChooser jfc = new JFileChooser();
        jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        jfc.showDialog(new JLabel(), "选择" + name);
        File dir = jfc.getSelectedFile();
        if (dir != null) {
            field.setText(dir.getAbsolutePath());
        }
    }

    private void generatePrj() {

        try {
            pgbGenerate.setStringPainted(true);
            pgbGenerate.setString("嗖一下，就完事");
            pgbGenerate.setIndeterminate(true);

            WingsInitProjectUtil.Info info = new WingsInitProjectUtil.Info();
            info.srcDir = new File(txtWingsPath.getText().trim());
            info.dstDir = new File(txtProjectPath.getText().trim());
            info.dstCodeName = txtCodeName.getText().trim();
            info.dstArtifactId = txtArtifactId.getText().trim();
            info.dstGroupId = txtGroupId.getText().trim();
            info.dstPackage = txtPackage.getText().trim();
            info.version = txtWingsVer.getText().trim();

            WingsInitProjectUtil.initProject(info, it -> message("复制:" + it));
            pgbGenerate.setString("完成了，进度条，逗你玩的");
        }
        catch (Exception e) {
            e.printStackTrace();
            message("出错了 " + e.getMessage());
            pgbGenerate.setString("出错了！");
            pgbGenerate.setIndeterminate(false);
        }
    }

    private void message(String txt) {
        txaMessage.append(txt);
        txaMessage.append("\n");
    }
}
